<template>
  <van-icon color="#ffa500" name="star" v-if="value" @click="onCollect" />
  <van-icon color="#777" name="star-o" v-else @click="onCollect" />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'ArticleCollect',
  components: {},
  model: {},
  props: {
    value: {
      type: Boolean,
      required: true
    },
    article_id: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      isLoading: false
    }
  },
  methods: {
    async onCollect () {
      try {
        if (this.value) {
          // 取消收藏
          await deleteCollect(this.article_id)
        } else {
          await addCollect(this.article_id)
        }
        this.$emit('input', !this.value)
        this.$toast.success(this.value ? '已取消' : '已收藏')
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
